﻿//*********************************************************************
//
//  文件名：PaymentController
//
//  作者： LW
//
//  作用：
//
//  日期：2015/9/24 10:29:01
//
//*********************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using YouTravel.DomainService;
using YouTravel.Framework.Enums;
using YouTravel.Framework.ResourceMessage;
using YouTravel.QueryModels;
using YouTravel.ViewModels;
using Framework.Common;
using Framework.Common.Paged;
using Framework.Common.Exceptions;
using System.Web.Script.Serialization;

namespace Application.WebAPI.Controllers
{
    /// <summary>
    /// PaymentController
    /// 付款管理
    /// </summary>
    [RoutePrefix("api/payment")]
    public class PaymentController : BaseController
    {
        IPaymentService paymentService;
        DataResult dataResult = new DataResult() { Code = ResponseStatusCode.Error, Msg = "" };

        /// <summary>
        /// 付款管理
        /// </summary>
        /// <param name="_paymentService"></param>
        public PaymentController(IPaymentService _paymentService)
        {
            this.paymentService = _paymentService;
        }

        /// <summary>
        /// 付款信息 列表
        /// </summary>
        /// <param name="queryCond"></param>
        /// <returns></returns>
        [HttpPost, Route("applylist")]
        public DataResult ApplyList([FromBody]PagedParam<PaymentQuery> queryCond)
        {
            queryCond.QueryDto.Sys_LoginUserID = this.LoginUser.ID;
            var list = this.paymentService.ApplyList(queryCond);
            dataResult.Code = ResponseStatusCode.Success;
            dataResult.Data = list;
            return dataResult;
        }

        /// <summary>
        /// 申领付款之前
        /// 获取专线供应商信息
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("applybeforeget")]
        public DataResult ApplyBeforeGet()
        {
            var user = LoginUser;
            if ((int)user.Type != 0 && user.Special == null)
                throw new DataOperationPermissions("非专线不能使用该功能");

            var data = this.paymentService.ApplyBeforeGet(user.Special.ID);

            dataResult.Code = ResponseStatusCode.Success;
            dataResult.Msg = BusinessResourceMessage.Success;
            dataResult.Data = data;

            return dataResult;
        }

        /// <summary>
        /// 申请付款之前
        /// 判定专线是否欠票
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("applybeforevalidatedebts")]
        public DataResult ApplyBeforeValidateDebts()
        {
            var user = LoginUser;
            if ((int)user.Type != 0 && user.Special == null)
                throw new DataOperationPermissions("非专线不能使用该功能");
            bool flag = this.paymentService.ApplyBeforeValidateDebts(user.Special.ID);

            dataResult.Code = ResponseStatusCode.Success;
            dataResult.Msg = BusinessResourceMessage.Success;
            dataResult.Data = flag;

            return dataResult;
        }

        /// <summary>
        /// 申请付款
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost, Route("apply")]
        public DataResult ToApplyPayment([FromBody]SimpleModel m)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();

            PaymentApplyViewModel model = js.Deserialize<PaymentApplyViewModel>(m.ID);
            model.Sys_LoginUserID = this.LoginUser.ID;
            this.paymentService.ToApplyPayment(model);
            dataResult.Code = ResponseStatusCode.Success;
            dataResult.Msg = BusinessResourceMessage.Success;
            return dataResult;
        }

        /// <summary>
        /// 付款详情
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        [HttpGet, Route("detail")]
        public DataResult PaymentDetail([FromUri]string ID)
        {
            var tmp = this.paymentService.PaymentDetail(int.Parse(ID));
            dataResult.Data = tmp;
            dataResult.Code = ResponseStatusCode.Success;
            return dataResult;
        }

        /// <summary>
        /// 付款管控 列表
        /// </summary>
        /// <param name="queryCond"></param>
        /// <returns></returns>
        [HttpPost, Route("managelist")]
        public DataResult ManageList([FromBody]PagedParam<PaymentQuery> queryCond)
        {
            queryCond.QueryDto.Sys_LoginUserID = this.LoginUser.ID;
            var list = this.paymentService.ManageList(queryCond);
            dataResult.Code = ResponseStatusCode.Success;
            dataResult.Data = list;
            return dataResult;
        }

        /// <summary>
        /// 确认
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost, Route("confirm")]
        public DataResult ToConfirmPayment([FromBody]PaymentConAbaViewModel model)
        {
            model.Sys_LoginUserID = this.LoginUser.ID;
            this.paymentService.ToConfirmPayment(model);
            dataResult.Code = ResponseStatusCode.Success;
            dataResult.Msg = BusinessResourceMessage.Success;
            return dataResult;
        }

        /// <summary>
        /// 废弃
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost, Route("abandoned")]
        public DataResult ToAbandonedPayment([FromBody]PaymentConAbaViewModel model)
        {
            model.Sys_LoginUserID = this.LoginUser.ID;
            this.paymentService.ToAbandonedPayment(model);
            dataResult.Code = ResponseStatusCode.Success;
            dataResult.Msg = BusinessResourceMessage.Success;
            return dataResult;
        }

        /// <summary>
        /// 更新打印页面访问次数
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost, Route("updateprintviewnum")]
        public DataResult ToUpdatePrintViewNum([FromBody]SimpleModel model)
        {
            this.paymentService.ToUpdatePrintViewNum(int.Parse(model.ID));
            dataResult.Code = ResponseStatusCode.Success;
            dataResult.Msg = BusinessResourceMessage.Success;
            return dataResult;
        }

        /// <summary>
        /// 单据打印信息
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        [HttpGet, Route("printdetail")]
        public DataResult PrintDetail([FromUri]string ID)
        {
            var tmp = this.paymentService.PrintDetail(int.Parse(ID), LoginUser.ID);
            dataResult.Data = tmp;
            dataResult.Code = ResponseStatusCode.Success;
            return dataResult;
        }
    }
}